<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="杰克小麻雀的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    从零开始免费搭建自己的博客(六)——三个站点一键发布博客 |  半亩方塘
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>
<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-从零开始免费搭建自己的博客(六)——三个站点一键发布博客"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  从零开始免费搭建自己的博客(六)——三个站点一键发布博客
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/01/24/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E5%85%AD)%E2%80%94%E2%80%94%E4%B8%89%E4%B8%AA%E7%AB%99%E7%82%B9%E4%B8%80%E9%94%AE%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/" class="article-date">
  <time datetime="2021-01-25T06:36:33.000Z" itemprop="datePublished">2021-01-24</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">博客搭建</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">3.4k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">16 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <blockquote>
<p>   ​    本文是博客搭建系列文章第六篇，其他文章链接：</p>
<ol>
<li>从零开始免费搭建自己的博客(一)——<a target="_blank" rel="noopener" href="https://yushuaige.github.io/2020/12/31/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2-%E4%B8%80-%E2%80%94%E2%80%94%E6%9C%AC%E5%9C%B0%E6%90%AD%E5%BB%BAhexo%E6%A1%86%E6%9E%B6/">本地搭建 Hexo 框架</a></li>
<li>从零开始免费搭建自己的博客(二)——<a target="_blank" rel="noopener" href="https://yushuaige.github.io/2021/01/01/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2-%E4%BA%8C-%E2%80%94%E2%80%94%E5%9F%BA%E4%BA%8E-GitHub-pages-%E5%BB%BA%E7%AB%99/">基于 GitHub pages 建站</a></li>
<li>从零开始免费搭建自己的博客(三)——<a target="_blank" rel="noopener" href="https://yushuaige.github.io/2021/01/02/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2-%E4%B8%89-%E2%80%94%E2%80%94%E5%9F%BA%E4%BA%8E-Gitee-pages-%E5%BB%BA%E7%AB%99/">基于 Gitee pages 建站</a></li>
<li>从零开始免费搭建自己的博客(四)——<a target="_blank" rel="noopener" href="https://yushuaigee.gitee.io/2021/01/11/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E5%9B%9B)%E2%80%94%E2%80%94%E7%BC%96%E5%86%99Markdown%E6%96%87%E7%AB%A0%E5%88%A9%E5%99%A8%20Typora/">编写Markdown文章利器 Typora</a></li>
<li>从零开始免费搭建自己的博客(五)——<a target="_blank" rel="noopener" href="https://yushuaigee.gitee.io/2021/01/14/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E4%BA%94)%E2%80%94%E2%80%94Typora%20+%20PicGo%20+%20GitHub%20Gitee%E5%9B%BE%E5%BA%8A/">Typora + PicGo + GitHub/Gitee图床</a></li>
<li><strong>从零开始免费搭建自己的博客(六)——<a target="_blank" rel="noopener" href="https://yushuaigee.gitee.io/2021/01/21/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E5%85%AD)%E2%80%94%E2%80%94%E4%B8%89%E4%B8%AA%E7%AB%99%E7%82%B9%E4%B8%80%E9%94%AE%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/">三个站点一键发布博客</a></strong></li>
<li>从零开始免费搭建自己的博客(七)——迁移 CSDN 博客到个人博客站点</li>
<li>从零开始免费搭建自己的博客(八)——博客网站个性化设置及优化</li>
</ol>
</blockquote>
<hr>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>本文目标：本地写好文章后，双击执行一个脚本，文章可以自动发布到自己的 github.io 、gitee.io 和 CSDN。</p>
<p>实现原理：</p>
<ol>
<li>写文章时自己在开头添加好title，date，tags等字段，用python实现文章的新旧版本替换。</li>
<li>配置好 Hexo 的配置文件，用shell脚本实现 <code>hexo g</code>和<code>hexo d</code>等操作，Python脚本实现Gitee Pages更新。</li>
<li>利用puppeteer模拟浏览器登录CSDN发布博客。</li>
<li>将前面三步的代码整合，使用bat脚本双击运行。</li>
</ol>
<h2 id="文件比较"><a href="#文件比较" class="headerlink" title="文件比较"></a>文件比较</h2><p>我平时在本地写文章保存在<code>E:\Markdown</code>目录，而 Hexo 保存在<code>D:\MyBlog</code>目录。发布 Hexo 之前需要先把文章拷贝到<code>D:\MyBlog\source\_posts</code>目录，有时候文章有修改还要重新拷贝覆盖。所以考虑用Python脚本实现拷贝文件，判断文件最后修改时间决定是否需要覆盖旧文章。</p>
<p>因为我们省略了<code>hexo create &quot;title&quot;</code>这一步，直接把文件拷贝到了<code>_posts</code>目录，所以写文章时需要确保在开头加上 title、date、tags、category信息，不然发布的文章会没有没有标题、发布时间、标签、分类信息，其实<code>hexo create</code>命令做的就是这件事。<strong>注意最后的空行一定要有</strong>。</p>
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">---</span><br><span class="line">title: 我的第一篇博客</span><br><span class="line">date: 2021-01-11 19:50:43</span><br><span class="line">tags: [博客搭建]</span><br><span class="line">---</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>如果是在 Typora 中编辑，直接在第一行输入<code>---</code>回车就行了。</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210121222437143.png" alt="image-20210121222437143"></p>
<p>Python 代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># copy_to_hexo.py</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">copy_to_hexo</span>():</span></span><br><span class="line">    local_list = os.listdir(LOCAL_ARTICLE_PATH)</span><br><span class="line">    hexo_list = os.listdir(HEXO_ARTICLE_PATH)</span><br><span class="line">    flag = <span class="literal">True</span></span><br><span class="line">    <span class="keyword">for</span> file <span class="keyword">in</span> local_list:</span><br><span class="line">        <span class="keyword">if</span> file <span class="keyword">in</span> IGNORE_LIST:</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        <span class="keyword">if</span> file.endswith(<span class="string">&#x27;.md&#x27;</span>):</span><br><span class="line">            local_version = os.path.join(LOCAL_ARTICLE_PATH, file)</span><br><span class="line">            hexo_version = os.path.join(HEXO_ARTICLE_PATH, file)</span><br><span class="line">            <span class="keyword">if</span> file <span class="keyword">not</span> <span class="keyword">in</span> hexo_list:</span><br><span class="line">                flag = <span class="literal">False</span></span><br><span class="line">                print(<span class="string">&quot;新增文章: %s...&quot;</span> % file,</span><br><span class="line">                      <span class="string">&quot;最后修改时间：%s&quot;</span> % TimeStampFormat(os.path.getmtime(local_version)))</span><br><span class="line">                shutil.copy(local_version, hexo_version)</span><br><span class="line">            <span class="keyword">elif</span> os.path.getmtime(local_version) &gt; os.path.getmtime(hexo_version):</span><br><span class="line">                flag = <span class="literal">False</span></span><br><span class="line">                print(<span class="string">&quot;更新文章: %s...&quot;</span> % file,</span><br><span class="line">                      <span class="string">&quot;上次修改时间：%s&quot;</span> % TimeStampFormat(os.path.getmtime(hexo_version)),</span><br><span class="line">                      <span class="string">&quot;最后修改时间：%s&quot;</span> % TimeStampFormat(os.path.getmtime(local_version)))</span><br><span class="line">                shutil.copy(local_version, hexo_version)</span><br><span class="line">    print(<span class="string">&#x27;文章无变化&#x27;</span> <span class="keyword">if</span> flag <span class="keyword">else</span> <span class="string">&#x27;更新完毕&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 时间格式标准化</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">TimeStampFormat</span>(<span class="params">timestamp</span>):</span></span><br><span class="line">    <span class="keyword">return</span> time.strftime(<span class="string">&#x27;%Y-%m-%d %H:%M:%S&#x27;</span>, time.localtime(timestamp))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">IGNORE_LIST = [<span class="string">&#x27;欢迎使用Markdown编辑器.md&#x27;</span>]</span><br><span class="line">HEXO_ARTICLE_PATH = <span class="string">&#x27;D:/MyBlog/source/_posts&#x27;</span></span><br><span class="line">LOCAL_ARTICLE_PATH = <span class="string">&#x27;E:/Markdown&#x27;</span></span><br><span class="line"></span><br><span class="line">copy_to_hexo()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>运行效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210124222133145.png" alt="image-20210124222133145"></p>
<h2 id="发布Hexo到GitHub-Gitee"><a href="#发布Hexo到GitHub-Gitee" class="headerlink" title="发布Hexo到GitHub/Gitee"></a>发布Hexo到GitHub/Gitee</h2><p>本来以为需要把配置文件<code>deploy</code>参数改成 GitHub 地址 <code>hexo d</code>一次，再改成 Gitee 地址<code>hexo d</code>一次，后来看文档发现 Hexo 支持一次发布到多个仓库，只需要修改一下配置文件，然后<code>hexo g &amp;&amp; hexo d</code>就可以了。</p>
<h4 id="1修改Hexo配置文件"><a href="#1修改Hexo配置文件" class="headerlink" title="1修改Hexo配置文件"></a>1修改Hexo配置文件</h4><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">deploy:</span></span><br><span class="line">  <span class="attr">type:</span> <span class="string">git</span></span><br><span class="line">  <span class="attr">repo:</span></span><br><span class="line">      <span class="attr">github:</span> <span class="string">git@github.com:用户名/用户名.github.io.git,main</span></span><br><span class="line">      <span class="attr">gitee:</span> <span class="string">git@gitee.com:用户名/仓库名.git,master</span></span><br></pre></td></tr></table></figure>
<h4 id="shell发布脚本"><a href="#shell发布脚本" class="headerlink" title="shell发布脚本"></a>shell发布脚本</h4><p><kbd>Git Bash</kbd>支持直接运行 shell 脚本，只需要把下面代码保存为 <code>.sh</code>后缀的文件即可。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> deploy_hexo.sh</span></span><br><span class="line">cd /d/MyBlog</span><br><span class="line">pwd</span><br><span class="line"><span class="meta">#</span><span class="bash"> 白底黑字效果</span></span><br><span class="line">echo -e &quot;\033[47;30m&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;hexo g&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\033[0m&quot;</span><br><span class="line">hexo g</span><br><span class="line">echo -e &quot;\033[47;30m&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;hexo d&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\033[0m&quot;</span><br><span class="line">hexo d</span><br><span class="line">sleep 5</span><br><span class="line"><span class="meta">#</span><span class="bash"> 执行完毕不退出</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> <span class="built_in">exec</span> /bin/bash</span></span><br></pre></td></tr></table></figure>
<p>运行效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210124214628069.png" alt="image-20210124214628069"></p>
<h4 id="Gitee-Pages更新脚本"><a href="#Gitee-Pages更新脚本" class="headerlink" title="Gitee Pages更新脚本"></a>Gitee Pages更新脚本</h4><p>上一篇博客已经实现了用 Python 脚本自动更新 Gitee pages，这里直接拿来用就行了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># update_gitee_pages.py</span></span><br><span class="line"><span class="comment"># 注意: 更改25、36、52行的用户名密码为自己的Gitee的用户名密码，第45行的仓库名为图床仓库的名字</span></span><br><span class="line"><span class="comment"># 每处延时都有用，是我花了好长时间调试过的</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> pyppeteer <span class="keyword">import</span> launch</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">_update_gitee_pages</span>(<span class="params">usr_name, repo_name</span>):</span></span><br><span class="line">	browser = <span class="keyword">await</span> launch(devtools=<span class="literal">False</span>, dumpio=<span class="literal">True</span>, autoClose=<span class="literal">True</span>,</span><br><span class="line">                           args=[<span class="string">&#x27;--start-maximized&#x27;</span>,  <span class="comment"># 设置浏览器全屏</span></span><br><span class="line">                                 <span class="string">&#x27;--no-sandbox&#x27;</span>,  <span class="comment"># 取消沙盒模式，沙盒模式下权限太小</span></span><br><span class="line">                                 <span class="string">&#x27;--disable-infobars&#x27;</span>,  <span class="comment"># 关闭受控制提示</span></span><br><span class="line">                                 <span class="comment"># 设置ua</span></span><br><span class="line">                                 <span class="string">&#x27;--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36&#x27;</span></span><br><span class="line">                                 ],</span><br><span class="line">                           userDataDir=os.path.abspath(<span class="string">&#x27;./cookies&#x27;</span>))</span><br><span class="line">    page = <span class="keyword">await</span> browser.newPage()</span><br><span class="line">    <span class="comment"># 登录</span></span><br><span class="line">    <span class="keyword">await</span> page.goto(<span class="string">&#x27;https://gitee.com/login&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">2000</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">&#x27;登录&#x27;</span> <span class="keyword">in</span> <span class="keyword">await</span> page.title():</span><br><span class="line">        <span class="keyword">await</span> page.<span class="built_in">type</span>(<span class="string">&#x27;#user_login&#x27;</span>, <span class="string">&#x27;用户名&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> page.<span class="built_in">type</span>(<span class="string">&#x27;#user_password&#x27;</span>, <span class="string">&#x27;密码&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;Enter&#x27;</span>)</span><br><span class="line">        print(<span class="string">&#x27;使用账号密码登录成功...&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> page.waitFor(<span class="number">2000</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">&#x27;使用cookies缓存登录成功...&#x27;</span>)</span><br><span class="line">    <span class="comment"># 更新</span></span><br><span class="line">    <span class="keyword">await</span> page.goto(<span class="string">&#x27;https://gitee.com/%s/%s/pages&#x27;</span> % (usr_name, repo_name))</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">2000</span>)</span><br><span class="line">    page.on(<span class="string">&#x27;dialog&#x27;</span>, <span class="keyword">lambda</span> dialog: asyncio.ensure_future(_handle_dialog(page, dialog)))</span><br><span class="line">    <span class="keyword">await</span> page.click(<span class="string">&#x27;#pages-branch &gt; div.button.orange.redeploy-button.ui.update_deploy&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">20000</span>)</span><br><span class="line">    print(<span class="string">&#x27;更新 Gitee Pages %s 成功...&#x27;</span> % repo_name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">_handle_dialog</span>(<span class="params">page, dialog</span>):</span></span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">2000</span>)</span><br><span class="line">    print(<span class="string">&#x27;点击确定更新&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> dialog.accept()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">update_gitee_pages</span>(<span class="params">usr_name, repo_name</span>):</span></span><br><span class="line">    asyncio.get_event_loop().run_until_complete(_update_gitee_pages(usr_name, repo_name))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    update_gitee_pages(<span class="string">&#x27;用户名&#x27;</span>, <span class="string">&#x27;仓库名&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>运行效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210124214209633.png" alt="image-20210124214209633"></p>
<h2 id="发布到CSDN"><a href="#发布到CSDN" class="headerlink" title="发布到CSDN"></a>发布到CSDN</h2><p>最不好处理的是自动发布文章到 CSDN ，据说 CSDN 官方之前是提供了发布文章的接口的，后来关闭了。一文多发这个功能对于做自媒体的人来说需求很大，于是有了  <a target="_blank" rel="noopener" href="https://openwrite.cn/">OpenWrite</a>这种一文多发平台，还有开源的<a target="_blank" rel="noopener" href="https://github.com/crawlab-team/artipub">ArtiPub</a>。各大平台都没有提供发布文章的官方接口，这种一分多发平台想必也是通过模拟 http 请求来实现自动发文。</p>
<p>本文利用 Python 的第三方库 <a target="_blank" rel="noopener" href="https://github.com/pyppeteer/pyppeteer">puppeteer</a> 操作无头浏览器模拟登陆 CSDN 发布文章，类似 Selenium，更加接近真实操作，防止被网站检测到机器操作导致封号。最近发现 Go 语言的 <a target="_blank" rel="noopener" href="https://github.com/go-rod/rod">go-rod</a> 库对于模拟浏览器操作更加好用，官方文档写的很详细，两者都不用另外下载 driver，相比于 Selenium 方便了很多，后面打算用 Golang 重新实现一版，今天先看下 Python 代码。<strong>注意这只是个 Demo，实现了自动发布一篇文章的功能，我们的需求是发布多篇文章，实际使用要稍作修改，实际使用的代码是这个 <a target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/scripts/post_to_csdn.py">post_to_csdn.py</a>)。</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># post_to_csdn_demo.py </span></span><br><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="keyword">from</span> pyppeteer <span class="keyword">import</span> launch</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">blog_name, title, content, tags, category</span>):</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    :param blog_name: 博客名字，自己博客主页url的最后部分</span></span><br><span class="line"><span class="string">    :param title: 文章标题</span></span><br><span class="line"><span class="string">    :param content: 文章内容</span></span><br><span class="line"><span class="string">    :param tags: 标签，多个用英文&quot;,&quot;隔开</span></span><br><span class="line"><span class="string">    :param category: 分类</span></span><br><span class="line"><span class="string">    :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    browser = <span class="keyword">await</span> launch(devtools=<span class="literal">False</span>, dumpio=<span class="literal">True</span>, autoClose=<span class="literal">True</span>,</span><br><span class="line">                           args=[<span class="string">&#x27;--start-maximized&#x27;</span>,  <span class="comment"># 设置浏览器全屏</span></span><br><span class="line">                                 <span class="string">&#x27;--no-sandbox&#x27;</span>,  <span class="comment"># 取消沙盒模式，沙盒模式下权限太小</span></span><br><span class="line">                                 <span class="string">&#x27;--disable-infobars&#x27;</span>,  <span class="comment"># 关闭受控制提示</span></span><br><span class="line">                                 <span class="comment"># 设置ua</span></span><br><span class="line">                                 <span class="string">&#x27;--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36&#x27;</span></span><br><span class="line">                                 ],</span><br><span class="line">                           userDataDir=os.path.abspath(<span class="string">&#x27;./cookies&#x27;</span>))</span><br><span class="line">    print(<span class="keyword">await</span> browser.userAgent())</span><br><span class="line">    pages_list = <span class="keyword">await</span> browser.pages()</span><br><span class="line">    page = pages_list[<span class="number">0</span>]</span><br><span class="line">    <span class="comment"># await page.setViewport(viewport=&#123;&#x27;width&#x27;: 1920, &#x27;height&#x27;: 1080&#125;)</span></span><br><span class="line">    <span class="comment"># 打开博客主页</span></span><br><span class="line">    <span class="keyword">await</span> page.goto(<span class="string">&#x27;https://blog.csdn.net/%s&#x27;</span> % blog_name)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">1000</span>)</span><br><span class="line">    <span class="comment"># 先找到已有文章</span></span><br><span class="line">     elements = <span class="keyword">await</span> page.querySelectorAll(<span class="string">&#x27;#articleMeList-blog &gt; div.article-list &gt; div &gt; h4&#x27;</span>)</span><br><span class="line">    article_list = []</span><br><span class="line">    <span class="keyword">for</span> article <span class="keyword">in</span> elements:</span><br><span class="line">        article = <span class="keyword">await</span> (<span class="keyword">await</span> article.getProperty(<span class="string">&#x27;textContent&#x27;</span>)).jsonValue()</span><br><span class="line">        article_list.append(<span class="built_in">str</span>(article).strip(<span class="string">&#x27;\n&#x27;</span>).split(<span class="string">&#x27;\n&#x27;</span>)[<span class="number">2</span>].strip())</span><br><span class="line">    print(<span class="string">&#x27;已有文章%d篇: &#x27;</span> % <span class="built_in">len</span>(article_list), article_list)</span><br><span class="line">    <span class="comment"># 点击 创作中心</span></span><br><span class="line">    <span class="keyword">await</span> page.click(</span><br><span class="line">        <span class="string">&#x27;#csdn-toolbar &gt; div &gt; div &gt; div.toolbar-container-right &gt; div &gt; div.toolbar-btn.toolbar-btn-write.csdn-toolbar-fl &gt; a&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">4000</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">&#x27;登录&#x27;</span> <span class="keyword">in</span> <span class="keyword">await</span> page.title():</span><br><span class="line">        print(<span class="string">&#x27;正在登录...&#x27;</span>)</span><br><span class="line">        <span class="comment"># 点击 CSDN App扫码</span></span><br><span class="line">        <span class="keyword">await</span> page.click(<span class="string">&#x27;#app &gt; div &gt; div &gt; div.main &gt; div.main-login &gt; div.main-select &gt; ul &gt; li:nth-child(1) &gt; a&#x27;</span>)</span><br><span class="line">        <span class="comment"># 获取登录二维码</span></span><br><span class="line">        img_element = <span class="keyword">await</span> page.querySelector(<span class="string">&#x27;#appqr &gt; span.app-code-wrap &gt; img&#x27;</span>)</span><br><span class="line">        img_src = <span class="keyword">await</span> (<span class="keyword">await</span> img_element.getProperty(<span class="string">&#x27;src&#x27;</span>)).jsonValue()</span><br><span class="line">        img_src = <span class="built_in">str</span>(img_src).split(<span class="string">&#x27;,&#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">        img_data = base64.b64decode(img_src)</span><br><span class="line">        img_name = <span class="string">&#x27;login.png&#x27;</span></span><br><span class="line">        <span class="keyword">if</span> os.path.exists(img_name):</span><br><span class="line">            os.remove(img_name)</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(img_name, <span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            f.write(img_data)</span><br><span class="line">        img = Image.<span class="built_in">open</span>(img_name)</span><br><span class="line">        img.show()</span><br><span class="line">    <span class="comment"># 等待登录成功，最多等5分钟</span></span><br><span class="line">    <span class="keyword">await</span> page.waitForSelector(</span><br><span class="line">        <span class="string">&#x27;#view-containe &gt; div.left_box &gt; div.left_box_top &gt; a.routerlink-bt.routerlink-bt-md &gt; span&#x27;</span>,</span><br><span class="line">        timeout=<span class="number">300</span> * <span class="number">1000</span>)</span><br><span class="line">    print(<span class="string">&#x27;已登录&#x27;</span>)</span><br><span class="line">    <span class="comment"># 点击 Markdown 编辑器</span></span><br><span class="line">    <span class="keyword">await</span> page.click(<span class="string">&#x27;#view-containe &gt; div.left_box &gt; div.left_box_top &gt; a.routerlink-bt.routerlink-bt-md &gt; span&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">3000</span>)</span><br><span class="line">    <span class="comment"># 切换到写文章页签</span></span><br><span class="line">    pages_list = <span class="keyword">await</span> browser.pages()</span><br><span class="line">    page = pages_list[-<span class="number">1</span>]</span><br><span class="line">    <span class="comment"># 选中标题输入框</span></span><br><span class="line">    print(<span class="string">&#x27;输入标题: %s&#x27;</span> % title)</span><br><span class="line">    title_input = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.layout &gt; div.layout__panel.layout__panel--articletitle-bar &gt; div &gt; div.article-bar__input-box &gt; input&#x27;</span>)</span><br><span class="line">    <span class="comment"># 清空原有内容</span></span><br><span class="line">    <span class="keyword">await</span> title_input.focus()</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.down(<span class="string">&#x27;Control&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;KeyA&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.up(<span class="string">&#x27;Control&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.up(<span class="string">&#x27;Backspace&#x27;</span>)</span><br><span class="line">    <span class="comment"># 输入标题内容</span></span><br><span class="line">    <span class="keyword">await</span> title_input.<span class="built_in">type</span>(title)</span><br><span class="line">    <span class="comment"># 选中文章输入框</span></span><br><span class="line">    print(<span class="string">&#x27;输入文章内容: %s&#x27;</span> % content)</span><br><span class="line">    content_input = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.layout &gt; div.layout__panel.flex.flex--row &gt; div &gt; div.layout__panel.flex.flex--row &gt; div.layout__panel.layout__panel--editor &gt; div.editor &gt; pre&#x27;</span>)</span><br><span class="line">    <span class="comment"># 清空原有内容</span></span><br><span class="line">    <span class="keyword">await</span> content_input.focus()</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.down(<span class="string">&#x27;Control&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;KeyA&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.up(<span class="string">&#x27;Control&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.up(<span class="string">&#x27;Backspace&#x27;</span>)</span><br><span class="line">    <span class="comment"># 输入文章内容</span></span><br><span class="line">    <span class="keyword">await</span> content_input.<span class="built_in">type</span>(content)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">2000</span>)</span><br><span class="line">    <span class="comment"># 点击 发布文章</span></span><br><span class="line">    <span class="keyword">await</span> page.click(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.layout &gt; div.layout__panel.layout__panel--articletitle-bar &gt; div &gt; div.article-bar__user-box.flex.flex--row &gt; button.btn.btn-publish&#x27;</span>)</span><br><span class="line">    <span class="comment"># 删除原来的标签，如果有的话</span></span><br><span class="line">    exist_tags = <span class="keyword">await</span> page.querySelectorAll(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div:nth-child(3) &gt; div &gt; div &gt; div &gt; span &gt; span &gt; i&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> exist_tags:</span><br><span class="line">        <span class="comment"># 每删一个，页面会有变化</span></span><br><span class="line">        tag = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">            <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div:nth-child(3) &gt; div &gt; div &gt; div &gt; span &gt; span &gt; i&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> tag.click()</span><br><span class="line">        <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">    <span class="comment"># 点击 添加文章标签</span></span><br><span class="line">    print(<span class="string">&#x27;添加标签: %s&#x27;</span> % tags)</span><br><span class="line">    add_tag = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div:nth-child(3) &gt; div &gt; div &gt; div &gt; button&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> add_tag.click()</span><br><span class="line">    <span class="comment"># 添加标签</span></span><br><span class="line">    tag_input = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div:nth-child(3) &gt; div &gt; div &gt; div.mark_selection_box &gt; div.mark_selection_box_header &gt; div &gt; div.el-input.el-input--suffix &gt; input&#x27;</span>)</span><br><span class="line">    tag_list = tags.split(<span class="string">&#x27;,&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> tag <span class="keyword">in</span> tag_list:</span><br><span class="line">        <span class="keyword">await</span> tag_input.<span class="built_in">type</span>(tag.strip())</span><br><span class="line">        <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">        <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;Enter&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">    <span class="comment"># 收起 添加文章标签</span></span><br><span class="line">    <span class="keyword">await</span> add_tag.click()</span><br><span class="line">    <span class="comment"># 添加分类</span></span><br><span class="line">    print(<span class="string">&#x27;添加分类: %s&#x27;</span> % category)</span><br><span class="line">    add_category = <span class="keyword">await</span> page.querySelector(<span class="string">&#x27;#tagList &gt; button&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> add_category.click()</span><br><span class="line">    category_input = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div:nth-child(4) &gt; div &gt; div &gt; input&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> category_input.<span class="built_in">type</span>(category)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;Enter&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">    <span class="comment"># 选择文章类型：原创</span></span><br><span class="line">    print(<span class="string">&#x27;文章类型: 原创&#x27;</span>)</span><br><span class="line">    select_box = <span class="keyword">await</span> page.querySelector(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div.inline-box &gt; div &gt; div&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> select_box.click()</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">500</span>)</span><br><span class="line">    <span class="comment"># 下拉框的 id 是个随机值，每次都不一样，通过按键曲线救国</span></span><br><span class="line">    <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;ArrowDown&#x27;</span>)</span><br><span class="line">    <span class="keyword">await</span> page.keyboard.press(<span class="string">&#x27;Enter&#x27;</span>)</span><br><span class="line">    <span class="comment"># 选择发布形式：公开 2, 私密 4, 粉丝可见 6, VIP可见 8</span></span><br><span class="line">    print(<span class="string">&#x27;发布形式: 公开&#x27;</span>)</span><br><span class="line">    flag = <span class="number">2</span></span><br><span class="line">    <span class="keyword">await</span> page.click(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__content &gt; div.form-entry.flex.form-entry__field-switch-box.overflow-unset.form-entry-marginBottom &gt; div &gt; div &gt; label:nth-child(%d)&#x27;</span> % flag)</span><br><span class="line">    <span class="comment"># 发布文章：保存为草稿 btn-c-blue, 发布文章 btn-b-red</span></span><br><span class="line">    print(<span class="string">&#x27;点击发布...&#x27;</span>)</span><br><span class="line">    flag = <span class="string">&#x27;btn-b-red&#x27;</span></span><br><span class="line">    <span class="keyword">await</span> page.click(</span><br><span class="line">        <span class="string">&#x27;body &gt; div.app.app--light &gt; div.modal &gt; div &gt; div.modal__inner-2 &gt; div.modal__button-bar &gt; button.button.%s&#x27;</span> % flag)</span><br><span class="line">    <span class="keyword">await</span> page.waitFor(<span class="number">3000</span>)</span><br><span class="line">    url_element = <span class="keyword">await</span> page.querySelector(<span class="string">&#x27;#alertSuccess &gt; div &gt; div.pos-top &gt; div:nth-child(4) &gt; a&#x27;</span>)</span><br><span class="line">    url = <span class="keyword">await</span> (<span class="keyword">await</span> url_element.getProperty(<span class="string">&#x27;href&#x27;</span>)).jsonValue()</span><br><span class="line">    <span class="keyword">await</span> browser.close()</span><br><span class="line">    print(<span class="string">&#x27;发布成功, 文章地址: %s&#x27;</span> % url)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">blog_name = <span class="string">&#x27;yushuaigee&#x27;</span></span><br><span class="line">title = <span class="string">&#x27;自动发布的第一篇文章&#x27;</span></span><br><span class="line">content = <span class="string">&#x27;自动发布的第一篇文章自动发布的第一篇文章自动发布的第一篇文章&#x27;</span></span><br><span class="line">tags = <span class="string">&#x27;博客搭建,自动发布文章&#x27;</span></span><br><span class="line">category = <span class="string">&#x27;博客搭建&#x27;</span></span><br><span class="line">asyncio.get_event_loop().run_until_complete(main(blog_name, title, content, tags, category))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>运行效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210124213855158.png" alt="image-20210124213855158"></p>
<h2 id="一键发布脚本！"><a href="#一键发布脚本！" class="headerlink" title="一键发布脚本！"></a>一键发布脚本！</h2><p>上面写的4个脚本一步一步实现了我们的每个小目标，对于 Windows 系统来说，可以使用 bat 脚本把它们整合在一起，完成“一键”发布的需求。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">:: post_my_blog.bat</span><br><span class="line">python copy_to_hexo.py</span><br><span class="line">D:\Git\git<span class="literal">-bash</span>.exe deploy_hexo.sh</span><br><span class="line">python update_gitee_pages.py</span><br><span class="line">python post_to_csdn.py</span><br><span class="line">pause</span><br></pre></td></tr></table></figure>
<p>写好文章后，直接双击<code>post_my_blog.bat</code>就可以发布到三个地方了。</p>
<p>最终运行效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/yushuaige/myblog@master/img/image-20210124223519915.png" alt="image-20210124223519915"></p>
<h2 id="后续"><a href="#后续" class="headerlink" title="后续"></a>后续</h2><p>本文解决了一文多发的问题，以前发布在 CSDN 上的博客就不用一篇一篇复制到自己的博客站点了，但是要想一键发布，需要先将 CSDN 上的文章先下载到本地，而且原来的文章是使用富文本编辑器写的，怎么转化成 Markdown 格式呢？下一篇文章将介绍这两个问题的解决方法。</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/01/24/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E5%85%AD)%E2%80%94%E2%80%94%E4%B8%89%E4%B8%AA%E7%AB%99%E7%82%B9%E4%B8%80%E9%94%AE%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E5%8A%A8%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/" rel="tag">自动发布博客</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/01/26/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E4%B8%83)%E2%80%94%E2%80%94%E8%BF%81%E7%A7%BB%20CSDN%20%E5%8D%9A%E5%AE%A2%E5%88%B0%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E7%AB%99%E7%82%B9/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            从零开始免费搭建自己的博客(七)——迁移 CSDN 博客到个人博客站点
          
        </div>
      </a>
    
    
      <a href="/2021/01/14/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%85%8D%E8%B4%B9%E6%90%AD%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%8D%9A%E5%AE%A2(%E4%BA%94)%E2%80%94%E2%80%94Typora%20+%20PicGo%20+%20GitHub%20Gitee%E5%9B%BE%E5%BA%8A/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">从零开始免费搭建自己的博客(五)——Typora + PicGo + GitHub/Gitee图床</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "zy6yBRj9KkWO2XxsT94n1DIW-gzGzoHsz",
    app_key: "auroBE2PQXkQ05CLi30SFv92",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2020-2021
        <i class="ri-heart-fill heart_icon"></i> 杰克小麻雀
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/favicon.ico" alt="半亩方塘"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>
<script type="text/javascript">
if(!/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
  document.write('<script type="text/javascript" src="/js/FunnyTitle.js"><\/script>');
  document.write('<script type="text/javascript" src="/js/snow.js"><\/script>');
}
</script>
</html>